let app = getApp();
import { getIndex } from "../../request/api";
let iconUrl = 'https://atush.vip/static/icons/';

Component({
    /**
     * 组件的属性列表
     */
    properties: {
        homeIsPulldown: {
          type: Boolean,
          value: false,
          observer: "onPullDownMethod"
        },
        onReachBottomCount: {
          type: Number,
          value: 1,
          observer: "onBottomMethod",
        }
    },
    /**
     * 组件的初始数据
     */
    data: {
        pageNo : 1,
        totalPage: 1,
        lang: null,
        langDate: null,
        showLoading:true,
        isProtocol: 0,
        secondHand: [],
        iconUrl: iconUrl,
        headerList: [
            // { id: 1, name: 'altun', icon: iconUrl +'altun.svg' },
            // { id: 2, name: 'oil', icon: iconUrl +'may.svg' },
            { id: 1, name: 'house', icon: iconUrl + 'oy.svg' },
            { id: 2, name: 'car', icon: iconUrl +'car.svg' },
            { id: 3, name: 'shop', icon: iconUrl +'dukkan.svg' },
            { id: 4, name: 'usedGoods', icon: iconUrl +'nimKaxBuyum.svg' },
            // { id: 7, name: 'delivery', icon: iconUrl +'paotui.svg' },
            { id: 5, name: 'jobs', icon: iconUrl +'hizmet.svg' },
            { id: 6, name: 'experts', icon: iconUrl +'ailaSaymanRimunt.svg' },
            { id: 7, name: 'service', icon: iconUrl +'sodaSetik.svg' },
            { id: 8, name: 'circleFriends', icon: iconUrl +'pengyq.svg' },
            
        ],
        mianList: [
            // { id: 1, name: 'car', icon: '/icon/car.svg' },
            // { id: 2, name: 'usedGoods', icon: '/icon/nimKaxBuyum.svg' },
            // { id: 3, name: 'foundItem', icon: '/icon/tepiwalhinim.svg' },
            // { id: 4, name: 'lostItem', icon: '/icon/yutturupKoyhan.svg' },
            // { id: 5, name: 'tutour', icon: '/icon/ailaOkutkuqi.svg' },
            { id: 1, name: 'experts', icon: iconUrl +'ailaSaymanRimunt.svg' },
            { id: 3, name: 'goodsPrice', icon: iconUrl +'malBaha.svg' },
            { id: 2, name: 'foundItem', icon: iconUrl +'tepiwalhinim.svg' },
            { id: 4, name: 'lostItem', icon: iconUrl +'yutturupKoyhan.svg' },
            { id: 5, name: 'service', icon: iconUrl +'sodaSetik.svg' },
            { id: 6, name: 'circleFriends', icon: iconUrl +'pengyq.svg' },
            // { id: 7, name: 'video', icon: '/icon/video.svg' },
            { id: 8, name: 'more', icon: iconUrl +'more.svg' }
        ],
        swiper: [],
        recommend: {},
        release: {},
        indicatorDots: true,

        // 导航栏和状态栏高度
        navigationBarAndStatusBarHeight:
            wx.getStorageSync('statusBarHeight') +
            wx.getStorageSync('navigationBarHeight') - 40 +
            'px'
    },

    /**
     * 组件的方法列表
     */
    methods: {
        // 获取首页内容
        getPost() {
            let status = 1
            let pageNo = this.data.pageNo
            // let data = { status, noLoading: true }
            let data = { status, pageNo }
            getIndex(res => {
                let resSecondHand = res.data.secondHand.data
                if (res.statusCode == 200) {
                  this.setData({ recommend: res.data, secondHand: this.data.secondHand.concat(resSecondHand), totalPage: res.data.secondHand.totalPage })
                }
                this.setData({ showLoading: false })
            }, data);
        },

        // 检查部分功能的登录权限
        someFun() {},

        // 二手品分页
        noMoreMethod(pull) {
          if(this.data.pageNo == this.data.totalPage) return
          let pageNo = this.data.pageNo += 1
          this.setData({pageNo : pageNo})
          this.getPost();
        },
        onPullDownMethod(value) {
          if (value == true) {
            this.noMoreMethod(true)
          }
        },
        onBottomMethod(number) {
          // 当lessons滚到页面底部时触发
          this.noMoreMethod(false)
        },

        // 协议同意
        protocolBtn(){
            wx.setStorage({
                key: 'isProtocol',
                data: 1
            })
            this.setData({ isProtocol: 1 })
        },

        // 检查是否同意协议
        getProtocol() {
           let protocol = wx.getStorageSync('isProtocol')
           this.setData({ isProtocol: protocol })
        },

        // 轮播图往详细
        onSwiper(item) {
            let itemId = item.currentTarget.dataset.item.id
            wx.navigateTo({
                url: '/pages/home/published/published?itemId=' + itemId
            });
        },

        // 更多店铺
        onClickShopMore() {
            wx.navigateTo({
                url: '/pages/home/shop/shop'
            });
        },

        // 更多房屋
        onClickHouseMore() {
            wx.navigateTo({
                url: '/pages/home/house/house'
            });
        },

        // 更多工作
        onClickJobMore() {
            wx.navigateTo({
                url: '/pages/home/jobs/jobs'
            });
        },

        // 推荐工作详细
        onClickJobDetailed(item) {
            let itemId = item.currentTarget.dataset.item.id
            wx.navigateTo({
                url: '/pages/home/jobs/jobsDetailed/jobsDetailed?itemId=' + itemId
            });
        },

        // 更多汽车
        onClickCarMore() {
            wx.navigateTo({
                url: '/pages/home/car/car'
            });
        },

        // 推荐汽车详细
        onClickCarDetailed(item) {
            let itemId = item.currentTarget.dataset.item.id
            wx.navigateTo({
                url: '/pages/home/car/carDetailed/carDetailed?itemId=' + itemId
            });
        },

        /**
         * 推荐店铺详细
         */
        onClickShopDetailed(item) {
            let itemId = item.currentTarget.dataset.item.id
            wx.navigateTo({
                url: '/pages/home/shop/shopDetailed/shopDetailed?itemId=' + itemId
            });
        },

        // 更多二手品
        onClickSecondHandMore() {
          wx.navigateTo({
              url: '/pages/home/secondHand/secondHand'
          });
        },

        // 首页二手品详细
        onClickCarDetailed(item) {
          let itemId = item.currentTarget.dataset.item.id
          wx.navigateTo({
              url: '/pages/home/secondHand/secondDetailed/secondDetailed?itemId=' + itemId
          });
      },

        /**
         * 
         */
        onClickHouseDetailed(item) {
            let itemId = item.currentTarget.dataset.item.id
            wx.navigateTo({
                url: '/pages/home/house/houseDetaild/houseDetaild?itemId=' + itemId
            });
        },

        /**
         * HeaderList 链接
         */
        onCilckHeaderList(event) {
            let index = event.currentTarget.dataset.index
            let checkAuth = false;
            switch (index) {
                case 0:
                    wx.navigateTo({
                        url: '/pages/home/house/house'
                    });
                    break;
                case 1:
                    wx.navigateTo({
                        url: '/pages/home/car/car'
                    });
                    break;
                case 2:
                    wx.navigateTo({
                        url: '/pages/home/shop/shop'
                    });
                    break;
                case 3:
                    wx.navigateTo({
                        url: '/pages/home/secondHand/secondHand'
                    });
                    break
                case 4:
                    wx.navigateTo({
                        url: '/pages/home/jobs/jobs'
                    });
                    break
                case 5:
                    wx.navigateTo({
                        url: '/pages/home/maintenance/maintenance'
                    });
                    break;
                case 6:
                    wx.navigateTo({
                        url: '/pages/home/buyAndSell/buyAndSell'
                    });
                    break;
                case 7:
                    let promise1 = new Promise(function(resolve, reject) {
                        app.login(res => {
                            resolve(res)
                        })
                    })
                    checkAuth = true
                    promise1.then(data => {
                        wx.navigateTo({ url: '/pages/detail/sendInfo/sendInfo' })
                    }, err => {
                        console.log("err: ", err)
                    })
                    break;
                default:
                    break;
            }
        },
        /**
         * MianList 链接
         */
        onCilcMainList(event) {
            let index = event.currentTarget.dataset.index
            let checkAuth = false;
            switch (index) {
                case 0:
                    wx.navigateTo({
                        url: '/pages/home/maintenance/maintenance'
                    });
                    break;
                case 1:
                    wx.navigateTo({
                        url: '/pages/home/price/price'
                    });
                    break;
                case 2:
                    wx.navigateTo({
                        url: '/pages/home/pickedUp/pickedUp'
                    });
                    break;
                case 3:
                    wx.navigateTo({
                        url: '/pages/home/throw/throw'
                    });
                    break;
                // case 4:
                //     wx.navigateTo({
                //         url: '/pages/home/tutor/tutor'
                //     });
                //     break;
                case 4:
                    wx.navigateTo({
                        url: '/pages/home/buyAndSell/buyAndSell'
                    });
                    break;
                case 5:
                    let promise1 = new Promise(function(resolve, reject) {
                        app.login(res => {
                            resolve(res)
                        })
                    })
                    checkAuth = true
                    promise1.then(data => {
                        wx.navigateTo({ url: '/pages/detail/sendInfo/sendInfo' })
                    }, err => {
                        console.log("err: ", err)
                    })
                    break;
                // case 10:
                //     wx.navigateTo({
                //         url: '/pages/home/video/video'
                //     });
                //     break;
                case 6:
                    wx.navigateTo({
                        url: '/pages/home/more/more'
                    });
                
                    break;
                default:
                    break;
            }
        },

        /**
         * newRelease
         */
        newRelease() {
            this.triggerEvent('goToFreshPage')
        },

        // 切换维吾尔语
        changeLangToUg() {
            app.changeLanguage("ugLang")
            this.setData({ lang: app.globalData.lang })
            wx.reLaunch({ url: "/pages/index/index" })
        },

        // 切换中文
        changeLangToZh() {
            app.changeLanguage("zhLang")
            this.setData({ lang: app.globalData.lang })
            wx.reLaunch({ url: "/pages/index/index" })
        },

        // 获取 ugLang
        getLang() {
            let data = wx.getStorageSync('currLangName')
            this.setData({ langDate: data })
        }
    },

    lifetimes: {
        ready(){
            this.setData({pageNo : 1});
        },
        attached: function() {
            this.setData({ lang: app.globalData.lang })
            this.getLang()
            this.getPost()
            this.getProtocol()
        }
    }
})
